<?php

namespace app\admin\controller;

use app\admin\model\UserWorkCount;
use app\admin\model\Vipuserlist;
use app\admin\model\ZoneList;
use app\common\controller\Backend;
use think\Db;
use think\exception\DbException;
use think\Model;
use think\response\Json;

class Userrightlist extends Backend
{
    /**
     * Anchor模型对象
     * @var \app\admin\model\Anchor
     */
    protected $model = null;
    protected $zone = null;
    protected $userWorkCount = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Userrightlist();

        $this->zone = new ZoneList();

        $this->userWorkCount = new UserWorkCount();

        $zoneList = $this->zone->all();
        $zoneData = [];
        foreach ($zoneList as $z) {
            $zoneData[$z->zone] = $z->zonename;
        }

        $grade = Db::table('user_right_config')->order("grade", "asc")->select();

        $gradeData = [];
        foreach ($grade as $v) {
            $data = [];
            $data['id'] = $v['grade'];
            $data['name'] = $v['gradeName'];

            $gradeData[] = $data;
        }

        $this->assignconfig("grade", $gradeData);
        $this->assign("grade", $grade);
        $this->assignconfig("zoneList", $zoneData);
    }

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $where = json_decode($this->request->get("filter", ''), true);
        if (isset($where['zone_list.zonename']) && !empty($where['zone_list.zonename'])) {
            $where['zone_list.zone'] = $where['zone_list.zonename'];
            unset($where['zone_list.zonename']);
        }

        if (isset($where['user_right_config.gradeName'])) {
            $where['user_right_config.grade'] = $where['user_right_config.gradeName'];
            unset($where['user_right_config.gradeName']);
        }

        $list = $this->model
            ->with(['zoneList', 'userRightConfig'])
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    public function info()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->userWorkCount
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    public function update()
    {
        Db::query("call sum_user_work_count('1')");

        Db::query("call userright_update()");

        $this->success("操作成功");
    }
}